package org.locker.device.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.locker.common.core.domain.R;
import org.locker.common.core.validate.EditGroup;
import org.locker.common.excel.utils.ExcelUtil;
import org.locker.common.idempotent.annotation.RepeatSubmit;
import org.locker.common.log.annotation.Log;
import org.locker.common.log.enums.BusinessType;
import org.locker.common.mybatis.core.page.PageQuery;
import org.locker.common.web.core.BaseController;
import org.locker.domain.bo.WithdrawBo;
import org.locker.domain.vo.WithdrawVo;
import org.locker.service.IWithdrawService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 提现
 *
 * @author winnie
 * @date 2023-10-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/device/withdraw")
public class WithdrawController extends BaseController {

    private final IWithdrawService withdrawService;

    /**
     * 查询提现列表
     */
    @SaCheckPermission("device:withdraw:list")
    @GetMapping("/list")
    public R<Page<WithdrawVo>> list(WithdrawBo bo, PageQuery pageQuery) {
        return R.ok(withdrawService.queryPageList(bo, pageQuery));
    }

    /**
     * 导出提现列表
     */
    @SaCheckPermission("device:withdraw:export")
    @Log(title = "提现", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WithdrawBo bo, HttpServletResponse response) {
        List<WithdrawVo> list = withdrawService.queryList(bo);
        ExcelUtil.exportExcel(list, "提现", WithdrawVo.class, response);
    }

    /**
     * 获取提现详细信息
     *
     * @param withdrawId 主键
     */
    @SaCheckPermission("device:withdraw:query")
    @GetMapping("/{withdrawId}")
    public R<WithdrawVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long withdrawId) {
        return R.ok(withdrawService.queryById(withdrawId));
    }

    /**
     * 修改提现
     */
    @SaCheckPermission("device:withdraw:edit")
    @Log(title = "提现", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WithdrawBo bo) {
        return toAjax(withdrawService.updateByBo(bo));
    }

    /**
     * 删除提现
     *
     * @param withdrawIds 主键串
     */
    @SaCheckPermission("device:withdraw:remove")
    @Log(title = "提现", businessType = BusinessType.DELETE)
    @DeleteMapping("/{withdrawIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] withdrawIds) {
        return toAjax(withdrawService.deleteWithValidByIds(List.of(withdrawIds), true));
    }
}
